package com.kedacom.ctsp.authority.controller;

import com.kedacom.ctsp.authority.constant.AccessLoggerConstant;
import com.kedacom.ctsp.authority.constant.AuthorizeConstant;
import com.kedacom.ctsp.authority.entity.Resource;
import com.kedacom.ctsp.authority.service.ResourceService;
import com.kedacom.ctsp.authority.vo.ResourceVO;
import com.kedacom.ctsp.authz.Authorize;
import com.kedacom.ctsp.logging.AccessLogger;
import com.kedacom.ctsp.web.controller.AbstractCrudTreeController2;
import com.kedacom.ctsp.web.controller.message.ResponseMessage;
import com.kedacom.ctsp.web.entity.page.PagerResult;
import com.kedacom.ctsp.web.entity.param.QueryParamEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.kedacom.ctsp.web.controller.message.ResponseMessage.ok;


/**
 * 资源控制器
 *
 * @author sunchenjie
 */
@RequestMapping("resource")
@ResponseBody
@AccessLogger(AccessLoggerConstant.Module.RESOURCE)
@Authorize(resources = AuthorizeConstant.AUTH_RESOURCE)
@Api(tags = "resource-manager", description = "资源管理")
@Slf4j
public class AuthorityResourceController extends AbstractCrudTreeController2<Resource, String, ResourceVO, ResourceService> {

    @GetMapping("/checkChildUnique/{name}")
    @ApiOperation("检测唯一性")
    public ResponseMessage<Boolean> checkChildUnique(@PathVariable String name, @RequestParam String parentId, @RequestParam String value) {
        return ok(getService().checkChildUnique(name, parentId, value));
    }

    @Authorize(resources = AuthorizeConstant.AUTH_RESOURCE_QUERY)
    @Override
    public ResponseMessage<PagerResult<ResourceVO>> list(QueryParamEntity param, HttpServletRequest request) {
        return super.list(param, request);
    }

    @Authorize(resources = AuthorizeConstant.AUTH_RESOURCE_UPDATE)
    @Override
    public ResponseMessage<String> updateByPrimaryKey(@PathVariable String id, @RequestBody ResourceVO data) {
        return super.patchByPrimaryKey(id, data);
    }

    @Authorize(resources = AuthorizeConstant.AUTH_RESOURCE_ADD)
    @Override
    public ResponseMessage<String> add(@RequestBody ResourceVO data) {
        return super.add(data);
    }

    @Authorize(resources = AuthorizeConstant.AUTH_RESOURCE_QUERY)
    @Override
    public ResponseMessage<List<ResourceVO>> listAsTree(QueryParamEntity param, HttpServletRequest request) {
        return super.listAsTree(param, request);
    }

    @Authorize(resources = AuthorizeConstant.AUTH_RESOURCE_QUERY)
    @Override
    public ResponseMessage<List<ResourceVO>> listAsTree(@PathVariable String id, QueryParamEntity param, HttpServletRequest request) {
        return super.listAsTree(id, param, request);
    }

    @Authorize(resources = AuthorizeConstant.AUTH_RESOURCE_QUERY)
    @Override
    public ResponseMessage<ResourceVO> traceToTop(@PathVariable String id, QueryParamEntity param, HttpServletRequest request) {
        return super.traceToTop(id, param, request);
    }

    @Authorize(resources = AuthorizeConstant.AUTH_RESOURCE_QUERY)
    @Override
    public ResponseMessage<PagerResult<ResourceVO>> listAllChildren(@PathVariable String id, QueryParamEntity param, HttpServletRequest request) {
        return super.listAllChildren(id, param, request);
    }
}


